//
//  UserTVC.m
//  豆瓣
//
//  Created by lanou3g on 15/12/9.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "UserTVC.h"
#import "UserDetailVC.h"
#import "DataBase.h"
#import "ActivityModel.h"
#import "UserDetail_Movie.h"
#import "DataBase_Movie.h"
#import "LandingVC.h"
#import "HTDataBase_DB.h"
#import "Model_DB.h"


@interface UserTVC ()
@property(nonatomic,strong)NSMutableArray *Myarray;
@property(nonatomic,assign)BOOL yes_no;

@end

@implementation UserTVC
NSString *user_CeLL_id = @"user_CeLL_id";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:user_CeLL_id];
   
    self.tableView.scrollEnabled = NO; //不让它滚动
  
//      [self setRightBarButtonItem]; //设置导航栏右按钮
    [self setRightBarButton];
    self.Myarray = [NSMutableArray arrayWithObjects:@"我的活动",@"我的电影",@"清除缓存", nil];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *state = [user objectForKey:@"State"];

    if ([state isEqualToString:@"YES"]) {
        self.navigationItem.rightBarButtonItem.title = @"注销";
    }else {
          self.navigationItem.rightBarButtonItem.title = @"登陆";
    }
    
  
}



- (void)setRightBarButton
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *state = [user objectForKey:@"State"];
    if ([state isEqualToString:@"YES"]) {
        self.navigationItem.rightBarButtonItem.title = @"注销";
    }
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonAction:)];
    
    

    
}

- (void)barButtonAction:(UIBarButtonItem *)sender
{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *state = [user objectForKey:@"State"];
    if ([state isEqualToString:@"YES"]) {
   
        
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定注销" preferredStyle:UIAlertControllerStyleAlert];
        
        
        [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            //注销
            [user setObject:@"NO" forKey:@"State"];
            self.navigationItem.rightBarButtonItem.title = @"登陆";
            
        }]];
        [self presentViewController:alertC animated:YES completion:nil];

        
       
    }else{
        
        //登陆
        LandingVC *land = [LandingVC new];
        UINavigationController *navigation_landing = [[UINavigationController alloc] initWithRootViewController:land];
        [navigation_landing.navigationBar setTintColor:nil];
        [navigation_landing.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        land.hidesBottomBarWhenPushed = YES;
        navigation_landing.navigationBar.translucent= NO;
        
        
        [self showDetailViewController:navigation_landing sender:nil];
        self.navigationItem.rightBarButtonItem.title = @"注销";
        
    }
    
  

    
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.Myarray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:user_CeLL_id forIndexPath:indexPath];
    
   
    cell.textLabel.text = self.Myarray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        
        
        
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *state = [user objectForKey:@"State"];
        
        
        
        
        if (![state isEqualToString:@"YES"]) {
            

            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
            
            
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            
            
            [self presentViewController:alertC animated:YES completion:nil];
            
        }else{
            
         
            UserDetailVC *userDetailVC = [[UserDetailVC alloc] init];;
            [self.navigationController pushViewController:userDetailVC animated:YES];
            
        }
 
        
    }else if (indexPath.row == 1){
        
        
        
        
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *state = [user objectForKey:@"State"];
        
        
        
        
        if (![state isEqualToString:@"YES"]) {
       
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
            
            
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            
            
            [self presentViewController:alertC animated:YES completion:nil];
            
        }else{
            
            
            UserDetail_Movie *user_movie = [[UserDetail_Movie alloc] init];
            
            [self.navigationController pushViewController:user_movie animated:YES];
            
            
        }
        
        
       
        
    }else{
        
        
        
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *state = [user objectForKey:@"State"];
        
        
        
        
        if (![state isEqualToString:@"YES"]) {
            
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
            
            
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            
            
            [self presentViewController:alertC animated:YES completion:nil];
            
        }else{
            
            
            
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除缓存?" preferredStyle:UIAlertControllerStyleAlert];
            
            
            [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
            [alertC addAction:[UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                
//                [[DataBase shareDataBaseHandle] openDB];
//                NSMutableArray *array = [[DataBase shareDataBaseHandle] selectStudent];
//                for (ActivityModel *model in array) {
//                    [[DataBase shareDataBaseHandle] deleteActivity:model.title];
//                }
//                
//                [[DataBase shareDataBaseHandle] closeDB];
                
                NSMutableArray *array = [[HTDataBase_DB shareData] selectAllFromTable:@"Activity"];
                for (Model_DB *model in array) {
                    
                    [[HTDataBase_DB shareData] deleteModel:model Table:@"Activity"];
                }
                
                NSMutableArray *array2 = [[HTDataBase_DB shareData] selectAllFromTable:@"Movie"];
                for (Model_DB *model in array2) {
                    
                    [[HTDataBase_DB shareData] deleteModel:model Table:@"Movie"];
                }
                
                
                
//                [[DataBase_Movie shareDataBaseHandle] openDB];
//                NSMutableArray *array_movie = [[DataBase_Movie shareDataBaseHandle] selectmovie];
//                for (MovieModel *model in array_movie) {
//                    [[DataBase_Movie shareDataBaseHandle] deleteMovie:model.title];
//                }
                
//                [[DataBase_Movie shareDataBaseHandle] closeDB];
                
              }]];
            
            [self presentViewController:alertC animated:YES completion:nil];
       }
   }
}




@end
